package com.wxh.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * @ClassName: WebMvcConfig
 * @Description: spring web mvc config web静态资源配置
 * @Author wxh
 * @Date: 2020/8/10 17:41
 * @Version V1.0.0
 * @Since 1.8
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     *  将本地磁盘路径映射为虚拟路径，从而访问本地磁盘上的文件
     * @param registry 资源处理注册对象
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Windows 10
        String osName = System.getProperty("os.name");
        // Windows系统
        if (osName.toLowerCase().startsWith("win")) {
            registry.addResourceHandler("/productPic/**").
                    addResourceLocations("file:D:/upload/productPic/");
        }else {//linux和mac系统
            registry.addResourceHandler("/productPic/**").
                    addResourceLocations("file:/upload/productPic/");
        }
        // 在Linux系统下
        // file:D:\idwork\daily
//        String workspace = "file:" + System.getProperty("user.dir");
        // file:D:\idwork, 说明daily文件和arcgis_js_api文件同级
//        String workspace_parent = workspace.substring(0, workspace.lastIndexOf(File.separatorChar));
//        registry.addResourceHandler("/arcgis_js_api/**")
//                .addResourceLocations(workspace + "/arcgis_js_api/", workspace_parent + "/arcgis_js_api/");
//        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
    }


}
